import math

def is_prime(x):
	div = [e for e in range(2, int(math.sqrt(x))+1) if x%e == 0]
	return len(div)==0

primes = [e for e in range(5, 10002, 2) if is_prime(e)]

_6k1 = [e for e in primes if (e-1)%6==0]
_6k_1 = [e for e in primes if (e+1)%6==0]

directions = [(1,0),(0,-1),(-1,0),(0,1)]

def gruenberger_path():
	
	def position(p):
		position.position = position.position[0] + directions[position.direction][0], \
					position.position[1] + directions[position.direction][1]
		if p in _6k1:
			position.direction = (position.direction-1)%4 # turn left
		elif p in _6k_1:
			position.direction = (position.direction+1)%4 # turn right
		return position.position # normal odd number

	position.position = 0,0
	position.direction = 0

	return {i: position(i) for i in range(3, 10002, 2) }

def crossing():
	recurrencies = dict()

	g = gruenberger_path()
	for key,value in g.items():
		recurrencies[value] = (recurrencies[value]+[key]) if value in recurrencies else [key]

	return sorted([x for x in recurrencies.values() if len(x)>1])

if __name__ == "__main__":
	c = crossing()
	for i in c:
		print("# :", i)
